<template>
  <div>
    <!-- 首页商品管理参数列表 -->
    <my-bread :breadList="breadList"></my-bread>
    <el-card>
      <el-alert
        title="注意:只允许为第三级分类设置参数!"
        type="warning"
        show-icon
      >
      </el-alert>
      <el-form>
        <el-form-item label="选择商品分类" :label-width="formLabelWidth">
          <el-cascader
            v-model="cate"
            :options="splist2"
            :props="cateFormdefaultProps"
            clearable
            @change="cateChage"
          ></el-cascader>
        </el-form-item>
      </el-form>

      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button
            type="primary"
            :disabled="disabled"
            @click="dialogAttrFormVisible = true"
            >添加参数</el-button
          >
          <el-table :data="manyList">
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-tag
                  :key="tag"
                  v-for="(tag, index) in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(scope.row, index)"
                >
                  {{ tag }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column
              label="参数名称"
              prop="attr_name"
            ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini"  @click="editBtn(scope.row)"
                  >编辑</el-button
                >
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="delBtn(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button
            type="primary"
            :disabled="disabled"
            @click="dialogAttrFormVisible = true"
            >添加属性</el-button
          >
          <el-table :data="onlyList">
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-tag
                  :key="tag"
                  v-for="(tag, index) in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(scope.row, index)"
                >
                  {{ tag }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column
              label="属性名称"
              prop="attr_name"
            ></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini"  @click="editBtn(scope.row)"
                  >编辑</el-button
                >
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="delBtn(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 添加动态参数或者静态属性 -->
    <el-dialog
      :title="activeName == 'many' ? '添加动态参数' : '添加静态属性'"
      :visible.sync="dialogAttrFormVisible"
    >
      <el-form :model="attributesForm">
        <el-form-item
          :label="activeName == 'many' ? '动态参数' : '静态属性'"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="attributesForm.attr_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAttrFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAttrFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { categories,categoriesAttributes,editCategoriesAttributes,addAttributes } from "@/http/goods.js";
export default {
  data() {
    return {
      breadList: [
        {
          path: "",
          title: "首页",
        },
        {
          path: "",
          title: "商品管理",
        },
        {
          path: "",
          title: "参数列表",
        },
      ],
      formLabelWidth: "120px",
      cateFormdefaultProps: {
        label: "cat_name",
        value: "cat_id",
        checkStrictly: true,
      },
      splist2: [],
      cate: [],
      activeName: "many",
      manyList: [], //动态参数
      onlyList: [], //静态属性
      dialogAttrFormVisible: false,
      attributesForm: {
      attr_name: "",
      },
    };
  },

  mounted() {
    this.getCateFn();
  },
  computed: {
    disabled() {
      return this.cate.length == 3 ? false : true;
    },
  },
  methods: {
        // 编辑按钮
    editBtn(val) {
      this.editDtCsForm = val
      this.dialogAttrFormVisible = true
    },
    // 确定编辑
    async addAttrFn() {
      await this.$API.editComitParmasApi({
        id: this.editDtCsForm.cat_id,
        attrId: this.editDtCsForm.attr_id,
        attr_name: this.editDtCsForm.attr_name,
        attr_sel: this.editDtCsForm.attr_sel,
        attr_vals: this.editDtCsForm.attr_vals.join(',')
      })
      this.getCateData()
      this.dialogAttrFormVisible = false
    },

        // 删除按钮
    delBtn(val) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await this.$API.deleteCsApi(val)
          console.log(res)
          this.categories()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    async getCateFn() {
      let res = await categories();
      this.splist2 = res.data;
    },
    //tab 切换
    handleClick(tab) {
      if (this.cate.length == 3) {
        this.getParams(tab.name);
      }
    },
    // 下拉选择分类 -
    cateChage() {
      if (this.cate.length == 3) {
        this.getParams(this.activeName);
      }
    },

    async getParams(sel) {
      let res = await categoriesAttributes(this.cate[this.cate.length - 1], {
        sel,
      });

      res.data.forEach((v, i) => {
        v.attr_vals = v.attr_vals ? v.attr_vals.split(",") : [];
        v.inputVisible = false; //是否显示input
        v.inputValue = ""; // input 框的值
      });
      if (sel == "many") {
        this.manyList = res.data;
      } else {
        this.onlyList = res.data;
      }
    },
    async handleClose(row, i) {
      row.attr_vals.splice(i, 1);
      var attr_vals = row.attr_vals.join(",");
      let res = await editCategoriesAttributes(
        this.cate[this.cate.length - 1],
        row.attr_id,
        {
          attr_name: row.attr_name,
          attr_sel: row.attr_sel,
          attr_vals: attr_vals,
        }
      );
    },
    // 点击 + new Tag 显示input
    showInput(row) {
      row.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    // input 框 按下回车
    async handleInputConfirm(row) {
      if (row.inputValue) {
        row.attr_vals.push(row.inputValue);
        var attr_vals = row.attr_vals.join(",");
        let res = await editCategoriesAttributes(
          this.cate[this.cate.length - 1],
          row.attr_id,
          {
            attr_name: row.attr_name,
            attr_sel: row.attr_sel,
            attr_vals: attr_vals,
          }
        );
      }

      row.inputVisible = false;
      row.inputValue = "";
    },

    async addAttrFn() {
      let res = await addAttributes(this.cate[this.cate.length - 1], {
        attr_name: this.attributesForm.attr_name,
        attr_sel: this.activeName,
      });
      this.getParams(this.activeName);
      this.dialogAttrFormVisible = false;
    },
  },
};
</script>

<style >
.el-cascader-panel {
  height: 300px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>